package yxy.game.pm2.module.wuchao.event;

import java.util.ArrayList;

import yxy.apple.util.RandomUtility;
import yxy.apple.util.RandomUtility.Weight;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.wuchaokeyrandom;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.message.WuchaoMessage;

/** 乌巢事件 宝箱 */
public class WuchaoEventChest extends WuchaoEvent {
	@Override
	public EventType getEventType() {
		return EventType.Chest;
	}

	@Override
	protected void _packageInfo(WuchaoMessage.WuchaoEvent.Builder builder) {

	}

	private Currency reward;

	public Currency getReward() {
		return reward;
	}

	public void setReward(Currency reward) {
		this.reward = reward;
	}

	public static WuchaoEventChest create() {
		WuchaoEventChest event = new WuchaoEventChest();

		wuchaokeyrandom data = DataCenter.getData(1, wuchaokeyrandom.class);
		Currency reward = event.getReward(data).reward;
		event.setReward(reward);

		return event;
	}

	private Reward getReward(wuchaokeyrandom data) {
		ArrayList<Reward> rewards = new ArrayList<>();
		for (int i = 0; i < data.getRewardtype().length; i++) {
			int type = data.getRewardtype()[i];
			int weight = data.getPercent()[i];
			if (type > 0 && weight > 0) {
				Currency reward = Currency.create(data.getRewardtype()[i], data.getRewardid()[i], data.getRewardnum()[i]);
				rewards.add(new Reward(reward, weight));
			}
		}
		return RandomUtility.randomWeight(rewards);
	}

	private class Reward implements Weight {
		Currency reward;
		int weight;

		private Reward(Currency reward, int weight) {
			this.reward = reward;
			this.weight = weight;
		}

		@Override
		public int getWeight() {
			return weight;
		}
	}

}
